package com.htzos.admin.system.controller;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.htzos.admin.system.bean.SystemMenuBase;
import com.htzos.admin.system.service.SystemMenuBaseService;
import com.htzos.common.tool.ExtQueryBean;

@Controller
@RequestMapping("/systemMenuBase")
public class SystemMenuBaseController{

    @Resource
    SystemMenuBaseService systemMenuBaseService;

    @RequestMapping("/list")
    public String list(HttpServletRequest request,SystemMenuBase condition, ExtQueryBean extQuery) {
    	request.setAttribute("tableName", "菜单管理");
        extQuery.setTotal(systemMenuBaseService.systemMenuBaseTotal(condition, extQuery));
        List<SystemMenuBase> list= systemMenuBaseService.systemMenuBaseList(condition, extQuery);
        request.setAttribute("list", list);
        request.setAttribute("condition", condition);
        request.setAttribute("extQuery", extQuery);
        return "admin/system/systemMenuBaseList";
    }

    @RequestMapping("/update")
    public ModelAndView update(HttpServletRequest request, SystemMenuBase systemMenuBase) {
        systemMenuBaseService.systemMenuBaseUpdate(systemMenuBase);
        return new ModelAndView(new RedirectView("list"), "useSession", "1");
    }

    @RequestMapping("/updatePage")
    public String updatePage(HttpServletRequest request, Integer id, Integer copy) {
    	request.setAttribute("tableName", "菜单管理");
    	
    	SystemMenuBase condition=new SystemMenuBase();
    	condition.setParentId(0);
    	List<SystemMenuBase> list= systemMenuBaseService.systemMenuBaseList(condition, null);
    	
        SystemMenuBase systemMenuBase = systemMenuBaseService.systemMenuBaseGet(id);
        request.setAttribute("list", list);
        request.setAttribute("systemMenuBase", systemMenuBase);
        if(copy != null && copy == 1){
        	request.setAttribute("copy", 1);
        	return "admin/system/systemMenuBaseAdd";
        }
        else
        	return "admin/system/systemMenuBaseUpdate";
    }

    @RequestMapping("/insert")
    public ModelAndView insert(HttpServletRequest request, SystemMenuBase systemMenuBase) {
        systemMenuBaseService.systemMenuBaseInsert(systemMenuBase);
        return new ModelAndView(new RedirectView("list"), "useSession", "1");
    }
    @RequestMapping("/addPage")
    public String addPage(HttpServletRequest request) {
    	SystemMenuBase condition=new SystemMenuBase();
    	condition.setParentId(0);
    	List<SystemMenuBase> list= systemMenuBaseService.systemMenuBaseList(condition, null);
    	request.setAttribute("list", list);
        return "admin/system/systemMenuBaseAdd";
    }

    @RequestMapping("/delete")
    public ModelAndView delete(HttpServletRequest request, SystemMenuBase systemMenuBase) {
    	systemMenuBase.setMenuId(Integer.valueOf(request.getParameter("id")));
        systemMenuBaseService.systemMenuBaseDelete(systemMenuBase);
        return new ModelAndView(new RedirectView("list"), "useSession", "1");
    }
    
    @RequestMapping("/move")
    public void move(HttpServletResponse response, Integer menuId, Integer parentId, Integer afterId) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
		try {
			systemMenuBaseService.systemMenuBaseMove(menuId, parentId, afterId);
			response.setStatus(HttpServletResponse.SC_OK);
        } catch (Exception e) {
        	e.printStackTrace();
        	response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
	}
}